#include "inputdrv_manage.hpp"
#include <map>
#include <string>
#include <iostream>
#include <boost/assert.hpp>
#include <boost/thread.hpp>

using namespace std;

static boost::mutex mutex; 


map<string, int> map_event_listener;

void input_manager_drv_register_event_listener(const char* name,  \
                 input_event_listener_t listener)
{
    string s(name);

    int addr = 0;
    addr = reinterpret_cast<int>(listener);
    
    boost::unique_lock<boost::mutex> lock(mutex);
    map_event_listener.insert(pair<string, int>(s, addr));
    
}


input_event_listener_t  input_manager_drv_get_event_listener(const char* name)
{
    map<string, int>::iterator iter; 
 
                #if 0
                for(iter = map_event_listener.begin();            \
                            iter != map_event_listener.end(); iter++) {

                    cout << "name                   :"  << iter->first << endl;
                    cout << "input_event_listener_t :"  << iter->second << endl;          
                }
                #endif

    iter = map_event_listener.find(name);

    if(iter == map_event_listener.end()){
        BOOST_ASSERT(false);
    }else{
        //cout << "iter : OK" << endl;
    }
        
    input_event_listener_t p =  reinterpret_cast<input_event_listener_t>(iter->second);  

    return p;
}













#if 0 

#include <boost/thread.hpp>
#include "autoinit.hpp"

void jack_input_event_listener(const char* input_name, struct input_event *event)
{

    cout << " Jack : Event listener ! " << endl;

}

void merry_input_event_listener(const char* input_name, struct input_event *event){

    cout << " merry : Event listener ! " << endl;

}

static void thread_run(const int &id){

    #if 1
    
        cout << " thread_run ! ** " << endl;
        input_event_listener_t pevent = NULL;


        input_manager_drv_register_event_listener("jack",  jack_input_event_listener);

        input_manager_drv_register_event_listener("merry",  merry_input_event_listener);

        pevent = input_manager_drv_get_event_listener("jack ");
        pevent(NULL, NULL);

        pevent = input_manager_drv_get_event_listener("merry");
        pevent(NULL, NULL);

                #else 
                    // map功能 测试
                    // RUN OK  OK  OK  OK  OK

                    int addr1 = 99;
                    input_event_listener_t p_in = reinterpret_cast<input_event_listener_t>(addr1);
                    input_manager_drv_register_event_listener("merry",  p_in);


                    input_event_listener_t p_out = input_manager_drv_get_event_listener("merry");

                    cout << "p_out = " << dec << reinterpret_cast<int>(p_out) << endl;

                    int addr2 = reinterpret_cast<int>(p_out);

                    cout << "addr = " << addr2 << endl;

                    if(addr1 == addr2)
                        cout << "map use : success!" << endl;
                    else
                        cout << "map use : fail   !" << endl;

    #endif
}


static  void demo_test(void)
{
	boost::thread t1(thread_run, 11);

    return;
}

auto_dev_init(demo_test);

#endif

